const db = uniCloud.database();
const _ = db.command;
module.exports = {
	_before: function() {

	},
	async index(hid) {
		let res = await db.collection("accompany-hospitals").doc(hid).get();
		
		let res2 = await db.collection("accompany-hospitals-service").where({
			hospitals_id: hid
		}).get()
		
		const serviceIds = res2.data.map(item => item.service_id);
		
		let res3 = await db.collection("accompany-service").where({
			_id: db.command.in(serviceIds)
		}).get()
		
		const result = {
			hospital: res.data[0],
			services: res3.data
		};
		
		return result;
	},
	async search(keyword){
		let res = await db.collection("accompany-hospitals").where({
			name: new RegExp(keyword)
		}).get()
		if(res){
			return res.data;
		}
		return null;
	}
}